//
//  VieitorLoginView.swift
//  新浪微博
//
//  Created by macbook on 16/3/7.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit
import SnapKit



//声明协议
@objc protocol VisitorLoginViewDelegate:NSObjectProtocol {

    //声明协议方法
    optional func userWillRegister()
    
    //登陆协议的方法
    
    func userWillLognin()


}








class VieitorLoginView: UIView {
    
    
    
    //声明代理对象
    

    
    weak var visitorDelegate: VisitorLoginViewDelegate?
    
    //判断登陆页面
    func setupInfoWith(title:String,imageName:String?){
    tipLabel.text = title
        
        if imageName != nil {
            //不是首页
            circleIcon.image = UIImage(named: imageName!)
            //隐藏大房子
            largeHouse.hidden = true
            //设置渐隐
            bringSubviewToFront(circleIcon)
        
        
        }else{
        //是首页
            //让圈圈转动
            statrAnimation()
        
        
        
        }
    
    }
    
    
    
    
    
    
    //让圈圈转动的方法
    private func statrAnimation(){
    
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.repeatCount = MAXFLOAT
        ani.duration = 20
        ani.toValue = 2 * M_PI
        
        //在动画结束或者页面处于非活动页面时动画效果不移除   默认是移除
        ani.removedOnCompletion = false
        //添加到图册
        circleIcon.layer.addAnimation(ani, forKey:nil)
    
    }
    
    
    
    
    //要自定义视图 首先找到入口
    override init(frame: CGRect) {
        //super
        super.init(frame: frame)
        
        //访客视图的自定义
        setUpUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
   
    
    
    
    
    
    //MARK: 自定义访客视图
    private func setUpUI() {
        
        addSubview(circleIcon)
        addSubview(maskImage)
        addSubview(largeHouse)
        
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
            //布局
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
            
        }
        
        
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
    
        tipLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.width.equalTo(230)
            make.height.equalTo(40)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            //为了不让按钮上下跳动（有的字数多有的字数少，当字数不一样时如果没有设定高度，按钮会上下跳动）
            make.width.equalTo(100)
            
        }
    
    
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            
        }
        
        maskImage.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(circleIcon.snp_center)
        }
    
        let value: CGFloat = 236.0 / 250.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
    
    //添加按钮点击事件
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
    
    
    }
    
    //MARK: 按钮监听事件
   @objc func loginBtnDidClick(){
    
    print(__FUNCTION__)
        
     visitorDelegate?.userWillLognin()
    }
    
   @objc func registerBtnClick(){
    
    print(__FUNCTION__)
    visitorDelegate?.userWillRegister?()
    }
    
    
    //懒加载遮罩视图
    private lazy var maskImage : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    //懒加载子视图
    private lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //大房子
    private lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    
    //中间的简介
    private lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.text = "关注一些人,回到这里看看有什么惊喜"
        l.textColor = UIColor.darkGrayColor()
        l.font = UIFont.systemFontOfSize(14)
        
        //自适应大小
        l.sizeToFit()
        //设置文字居中显示
        l.textAlignment = .Center
        //设置自动换行
        l.numberOfLines = 0
        
        return l
    }()
    
    //登录和注册按钮
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        //这是背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.sizeToFit()
        return btn
        
    }()
    
    
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        //这是背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.sizeToFit()
        return btn
        
    }()
    


}
